{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook is a really brief demo of the GrayscaleObservation introduced in the pull request. It is inspired by the observation model used in the vanilla DQN."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "sys.path.append(\"../\")\n",
    "from highway_env.envs.highway_env import HighwayEnv\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Playing with the DEFAULT_CONFIG"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For this example, I will use the HighwayEnv environment. However, in this environment, the constructor does not take a config dictionnary as an input, that is why I will directly edit the DEFAULT_CONFIG dictionnary. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "screen_width, screen_height = 84, 84\n",
    "\n",
    "HighwayEnv.DEFAULT_CONFIG[\"offscreen_rendering\"] = True\n",
    "HighwayEnv.DEFAULT_CONFIG[\"observation\"] =  {\n",
    "            \"type\": \"GrayscaleObservation\",\n",
    "            \"weights\": [0.2989, 0.5870, 0.1140],  #weights for RGB conversion\n",
    "            \"stack_size\": 4,\n",
    "            \"observation_shape\": (screen_width, screen_height)\n",
    "        }\n",
    "HighwayEnv.DEFAULT_CONFIG[\"screen_width\"] = screen_width\n",
    "HighwayEnv.DEFAULT_CONFIG[\"screen_height\"] = screen_height\n",
    "HighwayEnv.DEFAULT_CONFIG[\"scaling\"] = 1.75\n",
    "HighwayEnv.DEFAULT_CONFIG[\"policy_frequency\"] = 2  #Just to see the things move with a smaller timestep\n",
    "\n",
    "env = HighwayEnv()\n",
    "init_stack = env.reset()  # Gathering the first stack, with only one frame so far\n",
    "print(\"Environment's configuration:\")\n",
    "env.config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# env.render()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Stacking process"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, I will illustrate that the stack is incremented as expected by performing 3 new steps in the environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(ncols=2, nrows=2)\n",
    "fig.set_figheight(10)\n",
    "fig.set_figwidth(10)\n",
    "print(\"########################### Initial Stack ###########################\")\n",
    "for i in range(4):\n",
    "    row = int(i//2)\n",
    "    col = int(i%2)\n",
    "    ax[row, col].imshow(init_stack[:,:,i], cmap=plt.get_cmap('gray'))\n",
    "plt.show()\n",
    "\n",
    "for i in range(3):\n",
    "    out = env.step(1)\n",
    "    print(\"########################### New state ###########################\".format(i+1))\n",
    "    fig, ax = plt.subplots(ncols=2, nrows=2)\n",
    "    fig.set_figheight(10)\n",
    "    fig.set_figwidth(10)\n",
    "    for j in range(4):\n",
    "        row = int(j//2)\n",
    "        col = int(j%2)\n",
    "        ax[row, col].imshow(out[0][:,:,j], cmap=plt.get_cmap('gray'))\n",
    "        ax[row, col].set_title(\"frame #{}\".format(j))\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
